package com.changwu.cwbase.util;

import android.text.TextUtils;
import android.util.Log;

import com.changwu.cwbase.Controllers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static android.util.Log.ERROR;
import static android.util.Log.INFO;
import static android.util.Log.VERBOSE;
import static android.util.Log.WARN;

/**
 * @author ${王松清} QQ:764282246
 * @time 2017/11/29 8:39
 * @class describe
 * 工具日志类
 */
public class LogUtil {
    public static String customTagPrefix = Controllers.customTagPrefix;
    private static FileWriter fileWriter;
    public static String LOG_PATH = File.separator + "sdcard" + File.separator + customTagPrefix + File.separator + "log" + File.separator;
    public static boolean DEBUG = Controllers.sLogSwitch;
    public static String SPEAR_LINE =
            "------------------------------------------------------------------------------------------------------------------------------------------\n" +
                    "------------------------------------------------------------------------------------------------------------------------------------------\n" +
                    "------------------------------------------------------------------------------------------------------------------------------------------\n";

    /**
     * 日志工具类，无法构造对象。直接使用
     */
    private LogUtil() {
        throw new UnsupportedOperationException("No Impl");
    }

    //规定每段显示的长度
    private static int LOG_MAXLENGTH = 2000;

    /**
     * +
     * 日志工具类初始化
     */
    private static void init() {
        if (fileWriter == null) {
            File path = new File(LOG_PATH);
            if (!path.exists()) {
                path.mkdirs();
            }
            File logFile = new File(LOG_PATH + customTagPrefix + ".log");

            if (!logFile.exists()) {
                try {
                    logFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                if (fileWriter == null) {
                    fileWriter = new FileWriter(logFile, true);
                }
            } catch (IOException e) {
            }
        }
    }

    /**
     * 自动生成tag
     *
     * @return
     */
    private static String generateTag() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
        return tag;
    }

    /**
     * 警告输出信息
     *
     * @param content
     */
    public static void d(String content) {
        String tag = generateTag();
        d(tag, content);
    }

    /**
     * 警告输出信息
     *
     * @param tag
     * @param content
     */
    public static void d(String tag, String content) {
        printMsg(Log.DEBUG, tag, content);
    }


    /**
     * 错误日志信息
     *
     * @param content
     */
    public static void e(String content) {
        String tag = generateTag();
        e(tag, content);
    }

    /**
     * 错误日志信息
     *
     * @param tag
     * @param content
     */
    public static void e(String tag, String content) {
        printMsg(ERROR, tag, content);
    }

    /**
     * 信息级别输出信息
     *
     * @param content
     */
    public static void i(String content) {
        String tag = generateTag();
        i(tag, content);
    }

    /**
     * 信息级别输出信息
     *
     * @param bytes
     */
    public static void i(byte[] bytes) {
        i(bytes, 0, bytes.length);
    }

    /**
     * 信息级别输出信息
     *
     * @param bytes
     * @param start
     * @param len
     */
    public static void i(byte[] bytes, int start, int len) {
        StringBuilder sb = new StringBuilder();
        if (bytes != null) {
            for (int i = 0; i < len; i++) {
                sb.append(bytes[i + start]);
            }
        }
        i(sb.toString());
    }

    /**
     * 信息级别输出信息
     *
     * @param tag
     * @param content
     */
    public static void i(String tag, String content) {
        printMsg(INFO, tag, content);
    }


    /**
     * 提示级别输出信息
     *
     * @param content
     */
    public static void v(String content) {
        String tag = generateTag();
        v(tag, content);
    }

    /**
     * 提示级别输出信息
     *
     * @param tag
     * @param content
     */
    public static void v(String tag, String content) {
        printMsg(VERBOSE, tag, content);
    }


    /**
     * 提示级别输出信息
     *
     * @param content
     */
    public static void w(String content) {
        String tag = generateTag();
        w(tag, content);
    }

    /**
     * 警告级别输出信息
     *
     * @param tag
     * @param content
     */
    public static void w(String tag, String content) {
        printMsg(WARN, tag, content);
    }

    private static void printMsg(int type, String tag, String msg) {
        if (DEBUG) {
            //大于LOG_MAXLENGTH时
            while (msg.length() > LOG_MAXLENGTH) {
                Log.println(type, tag, msg.substring(0, LOG_MAXLENGTH));
                msg = msg.substring(LOG_MAXLENGTH);
            }
            //剩余部分
            Log.println(type, tag, msg);
        }
    }
}